import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 			= $Lazy.load(()=>import('#tp/page/page-container'))
const AddModal 		= $Lazy.state(()=>import('./cpt/add-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '组合名称',			name: 'group_name' },
			{ label: '药敏组合编号',		name: 'group_num' },
		],
		// 提交表单列表
		formData : [
			{ label: '组合名称',		name: 'group_name', 		half:true, required:true },
			{ label: '组合编号',		name: 'group_num', 			half:true, disabled:true },
			{ label: '排序',			name: 'sort', 				half:true, type:'number' },
			{ label: '描述',			name: 'description', 		full:true, type:'textarea' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		if(window.isInit){
			this.fetch()
		}
	}

	// paging
	fetch = () => $fn.fetch.call(this,'drug-group/index')
	
	// 表单
	cols = [
		{ title: '排序', 			dataIndex: 'sort', 			width:200 },
		{ title: '组合名称', 		dataIndex: 'group_name', 	width:200 },
		{ title: '药敏组合编号', 	dataIndex: 'group_num', 	width:200 },
		TableControls.controls.call(this, { width: 220})
	]
	// 表格操作按钮
	table_controls = (text,{ uuid }) => {
		return [
			// 编辑
			TableControls.edit.call(this, 'drug-group/info', { uuid },{
				click: ()=>{
					$fn.hideRows(this.state.formData, ['group_num'], false)
				 }
			}),				
			this.onDel([uuid]),														// 删除
			TableControls.click.call(this, '查看', ()=> this.addModal.open(uuid, true), { ghost:true} ),				// 查看
			Controls.click.call(this, '添加药敏', ()=> this.addModal.open(uuid), { ghost:true } )						// 添加药敏
		]
	}
	// 操作按钮
	controls = () => {
		return [
			// 添加
			Controls.add.call(this,{ click:()=>{
				const { formData } = this.state
				$fn.hideRows(formData, ['group_num'], true)
				this.setState({ formData })
			}}),			
			this.onDel(),						// 删除
		]
	}
	// 删除
	onDel = uuid => Delete.call(this, 'drug-group/del', { uuid, isKeys: !uuid, onComplete:()=>$fn.dataRemove('drug-group/index') })
	
	render(){
		return (
			<>
				<Page title='药敏组合' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this,{isInit:window.isInit}) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'drug-group/add', editApi:'drug-group/edit',onComplete:({ param })=>{$fn.dataClear()} }) }
				{/* 添加药敏 */}
				<AddModal onRef={ref=>this.addModal=ref} />
			</>
		)
	}
}

export default Index